package com.xbongbong.saas.service.toolbox.help;

import com.xbongbong.paas.toolbox.util.Arith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 产品库存计算
 *
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/6/13 15:05
 * @since v1.0
 */
@Component
public class ProductSearchHelp {

    private final static Logger LOG = LoggerFactory.getLogger(ProductSearchHelp.class);

    /**
     * 出入库单保存删除，仓库成本、子产品成本更新
     *
     * @param preStock   原库存
     * @param preCost    原成本
     * @param amendStock 修改库存 (正负)
     * @param amendCost  修改成本
     * @return 更新后成本
     */
    public Double calCost(Double preStock, Double preCost, Double amendStock, Double amendCost) {
        preStock = preStock == null ? 0D : preStock;
        preCost = preCost == null ? 0D : preCost;
        amendCost = amendCost == null ? 0D : amendCost;
        amendStock = amendStock == null ? 0D : amendStock;
        double totalStock = Arith.add(preStock, amendStock);
        double zero = 0D;
        if (Double.compare(totalStock, zero) == 0) {
            return preCost;
        }
        Double preTotalCost = Arith.mul(preStock, preCost);
        Double amendTotalCost = Arith.mul(amendStock, amendCost);
        Double totalCost = Arith.add(preTotalCost, amendTotalCost);
        return Arith.div(totalCost, totalStock);
    }

    /**
     * 库存减少
     * @param preStock
     * @param preCost
     * @param amendStock
     * @param amendTotalCost
     * @return
     */
    public Double calCostByStockDown(Double preStock, Double preCost, Double amendStock, Double amendTotalCost) {
        preStock = preStock == null ? 0D : preStock;
        preCost = preCost == null ? 0D : preCost;
        amendTotalCost = amendTotalCost == null ? 0D : amendTotalCost;
        amendStock = amendStock == null ? 0D : amendStock;
        double totalStock = Arith.sub(preStock, amendStock);
        double zero = 0D;
        if (Double.compare(totalStock, zero) == 0) {
            return preCost;
        }
        Double preTotalCost = Arith.mul(preStock, preCost);
        Double totalCost = Arith.sub(preTotalCost, amendTotalCost);
        return Arith.div(totalCost, totalStock);
    }

    /**
     * 库存减少
     * @param preStock
     * @param preCost
     * @param amendStock
     * @param amendTotalCost
     * @return
     */
    public Double calCostByStockUp(Double preStock, Double preCost, Double amendStock, Double amendTotalCost) {
        preStock = preStock == null ? 0D : preStock;
        preCost = preCost == null ? 0D : preCost;
        amendTotalCost = amendTotalCost == null ? 0D : amendTotalCost;
        amendStock = amendStock == null ? 0D : amendStock;
        double totalStock = Arith.add(preStock, amendStock);
        double zero = 0D;
        if (Double.compare(totalStock, zero) == 0) {
            return preCost;
        }
        Double preTotalCost = Arith.mul(preStock, preCost);
        Double totalCost = Arith.add(preTotalCost, amendTotalCost);
        return Arith.div(totalCost, totalStock);
    }
}
